#!/usr/bin/perl

# TODO: less bad code

use File::Slurp;
do "./config.pl";
# Where to start
my $i = 5;
# Necessary files;
my @Order = read_file("official/order");
my $Tweet = read_file("official/template");
# Temp variable for the tweet
my $Temp = "0";
# Pages and path to post the image
my $Page;
my $Path;
my $Page1
my $Page2;

#Media ID to be posted
my $Media1ID;
my $Media2ID;

while(1) {

    $Page = $Order[$i];
    $Page =~ s/[a-z]//g;
    $Path = "official/" . $Order[$i];

    $Tweet =~ s/ page/\nPage/;
    $Tweet =~ s/ \(/\n(/;
    $Tweet =~ s/Pages/Pages: $Page/g;
    $Tweet =~ s/: $Temp//;
    chomp $Path;

    if($Order[$i] =~ / /) {
	# TODO less bad this
        $Page1 = $Order[$i];
        $Page2 = $Order[$i];
        $Page1 =~ s/ .*//;
        $Page2 =~ s/.* //;
        $Path1 = $Path;
        $Path2 = $Path;
        $Path1 =~ s/ .*//;
        $Path2 =~ s/.* //;

        $Media1ID = $nt->upload([$Path1,$Page1])->{media_id};
        $Media2ID = $nt->upload(["official/".$Path2,$Page2])->{media_id};
        $nt->update($Tweet, {media_ids=>"$Media1ID,$Media2ID"});
    } else {
        $Media1ID = $nt->upload([$Path,$Path])->{media_id};
        $nt->update($Tweet, {media_ids=>"$Media1ID"});
    }
    print $Tweet;
    $i++;
    $Temp = $Page;
    sleep 1;
}
